package com.igeek.handler;

import com.igeek.entity.Product;
import com.igeek.json.JsonResult;
import com.igeek.service.ProductService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("product")
public class ProductHandler {

    @Resource
    private ProductService productService;

    @GetMapping("findAll")
    public JsonResult findAll(){
        JsonResult jsonResult = new JsonResult();
        List<Product> productList = productService.findAll();
        jsonResult.setState("success");
        jsonResult.setData(productList);
        return jsonResult;
    }

    @GetMapping("findByTypeId")
    public JsonResult findByTypeId(Integer typeId){
        System.out.println("typeId="+typeId);
        JsonResult jsonResult = new JsonResult();
        List<Product> productList = productService.findByTypeId(typeId);
        jsonResult.setState("success");
        jsonResult.setData(productList);
        return jsonResult;
    }

    @GetMapping("findByOption")
    public JsonResult findByOption(String brand,String location){
        System.out.println("brand="+brand+"\tlocation="+location);
        if(brand == ""){
            brand = null;
        }
        if(location == ""){
            location = null;
        }
        JsonResult jsonResult = new JsonResult();
        List<Product> productList = productService.findByOption(brand,location);
        jsonResult.setState("success");
        jsonResult.setData(productList);
        return jsonResult;
    }
}
